package com.rightsidetech.commom.utils.gps;

/**
 * 坐标转换工具
 */
public class GPSUtil {
    public static void main(String[] args) {
        Long gpsLat= Long.valueOf(30285073);
        Long gpsLong= Long.valueOf(120152412);
        LatLonPoint latLonPoint=toGPSPoint(gpsLat/10e5,gpsLong/10e5);
        System.out.println("高德转GPS:"+latLonPoint);
    }
    private final static double a = 6378245.0; // 长半轴
    private final static double pi = 3.14159265358979324; // π
    private final static double ee = 0.00669342162296594323; // e²

    private static double EARTH_RADIUS = 6378.137;

    /**
     * GCJ-02 to WGS-84,高德转GPS
     */
    public static LatLonPoint toGPSPoint(double latitude, double longitude) {
        LatLonPoint dev = calDev(latitude, longitude);
        double retLat = latitude - dev.getLatitude();
        double retLon = longitude - dev.getLongitude();
        for (int i = 0; i < 1; i++) {
            dev = calDev(retLat, retLon);
            retLat = latitude - dev.getLatitude();
            retLon = longitude - dev.getLongitude();
        }
        return new LatLonPoint(retLat, retLon);
    }

    /**
     *  WGS-84 to GCJ-02 ,GPS转高德
     */
    public static LatLonPoint toGCJoint(double latitude, double longitude) {
        LatLonPoint dev = calDev(latitude, longitude);
        double retLat = latitude + dev.getLatitude();
        double retLon = longitude + dev.getLongitude();
        return new LatLonPoint(retLat, retLon);
    }


    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度获取距离(单位：米)(GPS)
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static double getDistance(double lat1, double lng1, double lat2,
                                     double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s*1000;
        return s;
    }

    // 计算偏差
    private static LatLonPoint calDev(double wgLat, double wgLon) {
        if (isOutOfChina(wgLat, wgLon)) {
            return new LatLonPoint(0, 0);
        }
        double dLat = calLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = calLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        return new LatLonPoint(dLat, dLon);
    }

    // 判断坐标是否在国外
    private static boolean isOutOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    // 计算纬度
    private static double calLat(double x, double y) {
        double resultLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        resultLat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        resultLat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        resultLat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return resultLat;
    }

    // 计算经度
    private static double calLon(double x, double y) {
        double resultLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        resultLon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        resultLon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        resultLon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return resultLon;
    }

    /**
     * 判断是否在多边形区域内
     * @param pointLon 要判断的点的纵坐标
     * @param pointLat 要判断的点的横坐标
     * @param lon 区域各顶点的纵坐标数组
     * @param lat 区域各顶点的横坐标数组
     * @return
     */
    public static boolean isInMaxArea(double pointLon, double pointLat, double[] lon,double[] lat) {
        // 获取区域横纵坐标最大值和最小值
        double temp = 0.0;
        for (int i = 0; i < lon.length; i++) {
            for (int j = 0; j < lon.length - i - 1; j++) {
                if (lon[j] > lon[j + 1]) {
                    temp = lon[j];
                    lon[j] = lon[j + 1];
                    lon[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < lat.length; i++) {
            for (int j = 0; j < lat.length - i - 1; j++) {
                if (lat[j] > lat[j + 1]) {
                    temp = lat[j];
                    lat[j] = lat[j + 1];
                    lat[j + 1] = temp;
                }
            }
        }

        // 如果在最值组成的区域外，不在重点区域内
        return (pointLon < lon[0] || pointLon > lon[lon.length - 1] || pointLat < lat[0]
                || pointLat > lat[lat.length - 1]);
    }


}
